<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        * {
            margin: 0;
            padding: 0;
        }

        #app {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
            width: 100%;
            height: 80%;
        }

        #app .searchMainBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: flex-start;
            margin: 100px auto;
            height: 50px;
        }

        #app .searchMainBox .searchbox {
            width: 432px;
            height: 32px;
        }

        #app .searchMainBox .searchbox .search {
            display: block;
            float: left;
            width: 350px;
            height: 30px;
            font-size: 16px;
            border: 1px solid #379bcf;
            outline: none;
        }

        #app .searchMainBox .searchbox .searchbtn {
            display: block;
            float: left;
            height: 32px;
            width: 80px;
            background-color: #379bcf;
            color: #fff;
            border: none;
            outline: none;
            cursor: pointer;
        }

        #app .searchMainBox .btn {
            margin-top: 5px;
        }

        #app .searchMainBox .btn li {
            margin-right: 10px;
            display: block;
            float: left;
            text-align: left;
            list-style: none;
            font-size: 13px;
            color: #8a8a8a;
            cursor: pointer;
        }

        #app span {
            position: absolute;
            left: 15%;
            top: 450px;
            font-size: 16px;
        }

        #app .weatherCardBox {
            margin: 0 auto;
            width: 100%;
            position: relative;
            top: 0px;
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: center;
        }

        #app .weatherCardBox .weatherCard {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-evenly;
            width: 200px;
            height: 230px;
            text-align: center;
        }

    </style>
    <script src="vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>

    <div id="app">

        <div class="searchMainBox">

            <div class="searchbox">
                <input type="text" class="search" v-model="city" @keyup.enter="prt">
                <input type="button" class="searchbtn" value="搜索" @click="prt">
            </div>

            <ul class="btn">
                <li v-for="city in mainCitys" @click="hotCity(city)">{{city}}</li>
            </ul>

        </div>

        <div class="weatherCardBox" v-show="isShow">

            <div class="weatherCard" v-for="forecast in weatherInfo.forecast">
                <h1>{{forecast.type}}</h1>
                <p>{{forecast.low}} ~ {{forecast.high}}</p>
                <p>{{forecast.date}}</p>
            </div>

        </div>

    </div>
    
    <script>

        var app = new Vue({
            el: "#app",
            data: {
                city: "",
                isShow: false,
                weatherInfo: {},
                mainCitys: ["北京","上海","广州","深圳"]
            },
            methods: {
                prt: async function() {
                    this.getWeather();
                },
                hotCity: async function(city) {
                    this.city = city;
                    this.getWeather();
                },
                getWeather: async function() {
                    if(typeof this.city==='string' && this.city.length>0) {
                        try {
                            let reqorg = "http://wthrcdn.etouch.cn/weather_mini";
                            let req = reqorg + "?city=" + this.city;
                            let weatherInfoJson = await axios.get(req);
                            if(typeof weatherInfoJson.data.data === 'undefined') console.log("该城市不存在");
                            else {
                                this.weatherInfo = weatherInfoJson.data.data;
                                this.isShow = true;
                            }
                        } catch(err) {
                            console.log(err);
                        }
                    }
                    else {
                        console.log("请发送请求");
                    }
                }
            }
        })

    </script>

</body>
</html>